package com.asia.algorithmcode.array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @DESCRIPTION: 73
 * @USER: wanfu
 * @DATE: 2025/3/21 星期五 10:53
 */
public class SetZeroes {

    public static void main(String[] args) {
        int[][] arr = new int[][]{
                {1, 2, 3, 0},
                {1, 10, 11, 12},
                {13, 14, 15, 1}
        };
        new SetZeroes().setZeroes(arr);

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        boolean r0 = false;
        boolean c0 = false;

        for (int i = 0; i < m; i++) {
            if (matrix[i][0] == 0) {
                r0 = true;
                break;
            }
        }
        for (int i = 0; i < n; i++) {
            if (matrix[0][i] == 0) {
                c0 = true;
                break;
            }
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 0) {
                    matrix[0][j] = matrix[i][0] = 0;
                }
            }
        }

        for (int i = 1; i < n; i++) {
            if (matrix[0][i] == 0) {
                for (int j = 0; j < m; j++) {
                    matrix[j][i] = 0;
                }
            }
        }

        for (int i = 1; i < m; i++) {
            if (matrix[i][0] == 0) {
                Arrays.fill(matrix[i], 0);
            }
        }
        if (c0) {
            Arrays.fill(matrix[0], 0);
        }
        if (r0) {
            for (int i = 0; i < m; i++) {
                matrix[i][0] = 0;
            }
        }
    }
}
